import cv2
import face_recognition
from tkinter import *
#准备人脸编码对应姓名列表
known_face_names = []
known_face_names2 = []
#准备人脸库的人脸编码列
known_face_encodings = []
known_face_encodings2 = []
#准备人脸信息列表
name_list = []
ls = []


def admission():
    face_id = entry1.get()
    print('\n 数据初始化中，请直视摄像机录入数据....')
    # 2. 捕获视频中图片
    vc = cv2.VideoCapture(0)
    while True:
        # 获取视频中每一帧的图片；
        ret, img = vc.read()
        # 3 发现人脸图片中人脸的位置
        faces = face_recognition.face_locations(img)
        if cv2.waitKey(1) == 32:
            for i in range(1,2):
                known_face_names.append(face_id)
                print(known_face_names)
                # 保存图像
                cv2.imwrite('FACE/' + str(face_id) + '.jpg',img)
                cv2.imshow('image', img)
                fo = open("face_names/face_id.csv", "a")
                fo.write(",".join(known_face_names) + "\n")
                fo.close()
                known_face_names.clear()
                print('录入完成，退出')

            # 关闭摄像头
            vc.release()
            # 销毁窗口
            cv2.destroyWindow('vdo')
            # 结束循环
            break

        else:
            for (top, right, bottom, left) in faces:
                # 画矩形
                cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
            cv2.imshow('vdo', img)

def checkFace():
    # 2. 捕获视频中图片known_face_names
    vc = cv2.VideoCapture(0)
    i = 0
    dao = 0
    fo = open("face_names/face_id.csv", "r")
    ls = []
    for line in fo:
        line = line.replace("\n","")
        ls.append(line.split(","))
    for face_id in ls:
        for id in face_id:
            known_face = cv2.imread('Face/' + str(id) + '.jpg')
            face_encoding = face_recognition.face_encodings(known_face)
            known_face_encodings2.append(face_encoding)
            known_face_names2.append(str(id))
    fo.close()


    while True:
        # 获取视频中每一帧的图片；
        ret, img = vc.read()
        if not ret:
            print('没有捕获到人脸')
            break
        # 3 发现人脸图片中人脸的位置
        locations = face_recognition.face_locations(img)

        # 3.1 图片中人脸进行编码
        face_encodings = face_recognition.face_encodings(img, locations)


        # 遍历 location ,face_encoding,识别图片中人脸
        # location:top,right,bottom,left
        for (top, right, bottom, left), face_encoding in zip(locations, face_encodings):
            # 4. 识别视频图片中人脸的姓名
            matchs = face_recognition.compare_faces(known_face_encodings2, face_encoding)
            name = 'unknown'
            for machs, known_name in zip(matchs, known_face_names2):
                if any(machs):
                    name = known_name
                    break

            # 4.1 标记人脸的位置
            cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
            # 4.2 标记人脸的姓名
            cv2.putText(img, name, (left, top - 20), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)

            fq = open("签到名单/签到次数.txt", "r")
            a = fq.read()
            if a == str(1):
                fc = open("签到名单/签到名单.csv", "r")
                for line in fc:
                    line = line.replace("\n", "")
                    ls = line.split(",")
                    for name_1 in ls:
                        if name_1 == name:
                            print("你已经签到过")
                        else:
                            fb = open("签到名单/签到名单.csv", "a+")
                            name_list.append(name)
                            fb.write(",".join(name_list) + "\n")
                            fb.close()
                            print("签到成功")
                            name_list.clear()
                            ls.clear()

            if a == str(0):
                fo = open("签到名单/签到名单.csv", "a+")
                name_list.append(name)
                fo.write(",".join(name_list) + "\n")
                a = str(1)
                fw = open("签到名单/签到次数.txt", "w")
                fw.write(str(a))
                print("签到成功")
                name_list.clear()
                fw.close()
            fq.close()
            fo.close()
            # 5.展示
        cv2.imshow('vidio', img)
        # 6 等待事件
        if cv2.waitKey(1) == 27:
            # 关闭摄像头
            vc.release()
            # 销毁窗口
            cv2.destroyAllWindows()
            # 结束循环
            break

win = Tk()
win.title('人脸识别签到系统')
win.geometry('300x200')



w=Label(win,text="当相机窗口打开后，按空格键录入人脸\nface_names 录入人脸名单\n签到完成后按Esc退出相机窗口\n签到成功后在程序目录\签到名单文件下查看签到名单\n系统暂不支持输入中文")
w.pack()
entry1 = Entry(win, width=50)
entry1.pack()
button = Button(win, text="输入名字，点我录入", command=admission)  # 收到消息执行这个函数
button2 = Button(win, text="签到", command=checkFace)
button.pack()  # 加载到窗体，
button2.pack()
win.mainloop()
